//
//  SRLoading.m
//  YWQF_APP
//
//  Created by 孙锐 on 2018/4/18.
//  Copyright © 2018年 孙锐. All rights reserved.
//

#import "SRLoading.h"

@implementation SRLoading{
    CGRect selfFrame;
    UIImageView *gifImageView;
    UIView *contentView;
    UIImageView *failImageView;
    UITapGestureRecognizer *tap;
}

- (instancetype)initWithFrame:(CGRect)frame andStyle:(SRLoadingStyleEnum)style andReloadBlock:(void(^)(void))block
{
    self = [super initWithFrame:frame];
    if (self) {
        self->selfFrame = frame;
        self.reload = block;
        if (style == SRLoadingStyleDefault) {
            self.backgroundColor = [UIColor whiteColor];

            [self addImages];
        }else if (style == SRLoadingStyleClearBack){
            contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            contentView.backgroundColor = UIColorFromRGB(0x333333);
            contentView.alpha = 0.3;
            [self addSubview:contentView];
            [self addAlphaImages];
        }
        [self getFailImage];

    }
    return self;
}

-(void)addAlphaImages{
    gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(321*ScreenScale, 500*ScreenScale, 600*ScreenScale, 600*ScreenScale)];
    gifImageView.animationImages = [self animationAlphaImages]; //获取Gif图片列表
    gifImageView.animationDuration = 1;     //执行一次完整动画所需的时长
    gifImageView.animationRepeatCount = 0;  //动画重复次数
    [gifImageView startAnimating];
    [self addSubview:gifImageView];
}

-(void)addImages{
    gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(321*ScreenScale, 500*ScreenScale, 600*ScreenScale, 600*ScreenScale)];
    gifImageView.animationImages = [self animationImages]; //获取Gif图片列表
    gifImageView.animationDuration = 1;     //执行一次完整动画所需的时长
    gifImageView.animationRepeatCount = 0;  //动画重复次数
    [gifImageView startAnimating];
    [self addSubview:gifImageView];
}

-(void)getFailImage{
    failImageView = [[UIImageView alloc] initWithFrame:CGRectMake(221*ScreenScale, 500*ScreenScale, 800*ScreenScale, 800*ScreenScale)];
    failImageView.image = image(@"loadFail");
    failImageView.hidden = YES;
    [self addSubview:failImageView];
}

-(void)stopAnimateding{
    gifImageView.hidden = YES;
    failImageView.hidden = NO;
    self->tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickToReload)];
    [self addGestureRecognizer:tap];
}

-(void)animatedAgain{
    gifImageView.hidden = NO;
    failImageView.hidden = YES;
    [self removeGestureRecognizer:self->tap];
}

-(void)turnToEmptyWithContent:(NSString *)content{
    gifImageView.hidden = YES;
    failImageView.hidden = YES;
    UIImageView *emptyImageView = [[UIImageView alloc]init];
    emptyImageView.image = image(@"emptyData");
    emptyImageView.frame = CGRectMake((selfFrame.size.width-200*ScreenScale)/2, 350*ScreenScale, 200*ScreenScale, 200*ScreenScale);
    [self addSubview:emptyImageView];
    
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.frame = CGRectMake(0, 660*ScreenScale, selfFrame.size.width, 47*ScreenScale);
    textLabel.font = font(47*ScreenScale);
    textLabel.text = content;
    textLabel.textColor = UIColorFromRGB(0xB2B2B2);
    textLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:textLabel];
}



-(void)clickToReload{
    self.reload();
    [self animatedAgain];
}

-(NSArray *)animationAlphaImages{
    NSFileManager *fielM = [NSFileManager defaultManager];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"alphaLoading" ofType:@"bundle"];
    NSArray *arrays = [fielM contentsOfDirectoryAtPath:path error:nil];
    
    NSMutableArray *imagesArr = [NSMutableArray array];
    for (NSString *name in arrays) {
        UIImage *image = [UIImage imageNamed:[(@"alphaLoading.bundle") stringByAppendingPathComponent:name]];
        if (image) {
            [imagesArr addObject:image];
        }
    }
    return imagesArr;
}

- (NSArray *)animationImages
{
    NSFileManager *fielM = [NSFileManager defaultManager];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"bundle"];
    NSArray *arrays = [fielM contentsOfDirectoryAtPath:path error:nil];
    
    NSMutableArray *imagesArr = [NSMutableArray array];
    for (NSString *name in arrays) {
        UIImage *image = [UIImage imageNamed:[(@"loading.bundle") stringByAppendingPathComponent:name]];
        if (image) {
            [imagesArr addObject:image];
        }
    }
    return imagesArr;
}



@end
